package com.bawei.a2010douyin.ui.register;

import androidx.appcompat.app.AppCompatActivity;
import okhttp3.MediaType;
import okhttp3.RequestBody;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.base.mvp.BaseActivity;
import com.bawei.a2010douyin.base.mvp.BaseMvpActivity;
import com.bawei.a2010douyin.base.utils.ARouterPath;
import com.bawei.a2010douyin.base.utils.ConstantUtils;
import com.bawei.a2010douyin.entity.RegisterEntity;
import com.bawei.a2010douyin.im.IMCallBack;
import com.bawei.a2010douyin.im.IMUtils;
import com.google.gson.Gson;

import java.util.HashMap;

@Route(path = ARouterPath.REGISTER_AROUTER_PATH)
public class RegisterActivity extends BaseMvpActivity<RegisterContract.IRegisterView,RegisterPresenter> implements RegisterContract.IRegisterView {
    private EditText etUsername;
    private EditText etPassword;
    private EditText etRepassword;
    @Override
    public int bindLayout() {
        return R.layout.activity_register;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        etRepassword = (EditText) findViewById(R.id.et_repassword);
    }

    @Override
    public RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    @Override
    public void initData() {

    }
    //结果
    @Override
    public void showRegister(RegisterEntity data) {
        //环信IM注册
        IMUtils.getInstance().register(data.getUsername(), data.getPassword(), new IMCallBack<String, String>() {
            @Override
            public void onOk(String s) {
                Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                //将数据回传值给登陆页面
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("username",data.getUsername());
                bundle.putString("password",data.getPassword());
                intent.putExtras(bundle);
                setResult( ConstantUtils.RESULT_CODE,intent);
                finish();
            }

            @Override
            public void onError(String s) {
                Toast.makeText(RegisterActivity.this, "环信注册是吧"+s, Toast.LENGTH_SHORT).show();
            }
        });





    }

    @Override
    public void onError() {
        super.onError();
        Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
    }

    /**
     * {
     *   "address": "string",
     *   "birth": "string",
     *   "email": "string",
     *   "icon": "string",
     *   "nickname": "string",
     *   "password": "string",
     *   "repassword": "string",
     *   "sex": "string",
     *   "username": "string"
     * }
     */
    public void register(View view) {
        HashMap<String, String> map = new HashMap<>();
        map.put("username",etUsername.getText().toString());
        map.put("password",etPassword.getText().toString());
        map.put("repassword",etRepassword.getText().toString());
        String s = new Gson().toJson(map);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), s);
        mPresenter.register(requestBody);
    }

}